﻿
<?php 

// error_reporting 
error_reporting(E_ALL ^ E_NOTICE);	

class Install
{
	/**
	* 构造
	*/
	function Install()
	{
		
	}
	
	/**
	* 目录校验
	*/
	function check_dirwrite($dir)
	{
		$tfile = "_test.txt";
		$fp = @fopen($dir . "/" . $tfile, "w");
		if (!$fp) {
			return false;
		}
		fclose($fp);
		$rs = @unlink($dir . "/" . $tfile);
		if ($rs) {
			return true;
		}
		return false;
	}
	
	function sql_split($sql)
	{
		$sql = preg_replace("/TYPE=(InnoDB|MyISAM)( DEFAULT CHARSET=[^; ]+)?/", "TYPE=\\1 DEFAULT CHARSET=utf8", $sql);
        
        $sql = str_replace("\r", "\n", $sql);
        $ret = array ();
        $num = 0;
        $queriesarray = explode(";\n", trim($sql));
        unset($sql);
        foreach ($queriesarray as $query) {
            $ret[$num] = '';
            $queries = explode("\n", trim($query));
            $queries = array_filter($queries);
            foreach ($queries as $query) {
                $str1 = substr($query, 0, 1);
                if ($str1 != '#' && $str1 != '-')
                    $ret[$num] .= $query;
            }
            $num++;
        }
        return ($ret);
	}
	
	function sql_execute($sql, $conn)
	{
		try
		{
			$sqls = $this->sql_split($sql);
			if (is_array($sqls)) {
				foreach ($sqls as $easql) {
					if (trim($easql) != '') {
						mysql_query($easql, $conn);
					}
				}
			} else {
				mysql_query($sqls, $conn);
			}
		}
		catch(Exception $ex)
		{
			print $ex->getMessage();
			return false;
		}
		
        return true;
	}

}


?>